<?php
namespace Admin\Controller;
class WorkflowController extends BaseController {
	public function index(){
        $object=M('Workflow');
        import('ORG.Util.Page');			//Page类的引入
        $count=$object->where($where)->count(); //获取符合条件的数据总数count
		$Page=new \Org\Util\Page($count, 10);			//实例化page类，传入数据总数和每页显示10条内容
		$show=$Page->show();
		$list=$object->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
		
		$list=$object->where()
		->field('bs_Workflow.*,b.showname as fname')
		->join('LEFT JOIN bs_form b       ON bs_Workflow.formid = b.id')
		->limit($Page->firstRow.','.$Page->listRows)->select();
		
        $this->assign('list',$list);
        $this->assign("page",$show);
        $this->display();
	}
	public function workflowToMode(){
        $object=M('Workflowtomode');
        import('ORG.Util.Page');			//Page类的引入
        $count=$object->where($where)->count(); //获取符合条件的数据总数count
		$Page=new \Org\Util\Page($count, 10);			//实例化page类，传入数据总数和每页显示10条内容
		$show=$Page->show();
		$list=$object->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
		
		$list=$object->where()
		->field('bs_Workflowtomode.*,m.name as mname,w.name as wname,n.name as nname,u.name as uname')
		->join('LEFT JOIN bs_module m     ON bs_Workflowtomode.moduleid = m.id')
		->join('LEFT JOIN bs_workflow w   ON bs_Workflowtomode.workflowid = w.id')
		->join('LEFT JOIN bs_workflownode n   ON bs_Workflowtomode.nodeid = n.id')
		->join('LEFT JOIN bs_workflownode u   ON bs_Workflowtomode.creaternodeid = u.id')
		->limit($Page->firstRow.','.$Page->listRows)->select();
		
        $this->assign('list',$list);
        $this->assign("page",$show);
        $this->display();
	}
	public function workflowToModeChoose(){
	    $workflowid = I('get.id');
		if($workflowid){
		  $this->assign('object',M('Workflowtomode')->where('workflowid='.$workflowid)->find());
		}
        $this->assign('workflowlist',M('Workflow')->where('')->select()); //工作流list
        $this->assign('nodelist',M('Workflownode')->where('workflowid='.$workflowid)->select());     //节点list
        $this->assign('modelist',M('Module')->where('')->select());     //模块list
        $this->assign('userlist',M('Workflownode')->where('')->select());     //创建人list
        $this->display('workflowToModeChoose');
	}
	public function workflowToModeChooseOperation(){
		$object = M('WorkflowToMode');
		$object -> create();
		$id = I('post.id');
		if($id){//修改
			if($Object->save()){
				$this->success('修改成功',U('workflowToModeUI'));
			}else{
				dump($Object->_sql());
				$this->error('修改失败');
			}
		}else{//新增
			if($Object->add()){
				$this->success('添加成功',U('workflowToModeUI'));
			}else{
				dump($Object->_sql());
				$this->error('添加失败');
			}
		}
		$this->assign('object',M('Workflowtomode')->where('workflowid='.$workflowid)->find());
	}
	public function workflowToModeUI(){
	    $workflowId = I('get.id');
	    $moduleId   = I('get.moduleId');
		if($workflowId){
			$object_wf = M('Workflow')->where('id='.$workflowId)->find();
			$object_md = M('Module')->where('id='.$moduleId)->find();
            $this->assign('formfieldlist',M('Formfield')->where('formid='.$object_wf->formid)->select());
            $this->assign('modelist',M('Formfield')->where('formid='.$object_md->formid)->select());
            $this->display('workflowToModeChoose');
		}
	}
	public function workflowToModeOperation(){
	    
	}
    public function editUI(){
		if(I('get.id')){
			$this->assign('object',M('Workflow')->where('id='.I('get.id'))->find());
		}
        $this->assign('formlist',M('Form')->where($where)->select());
        $this->display('editUI');
    }
    public function checkDiagram(){
        $id = I('get.id');
		if($id){
			$this->assign('object',M('Workflow')->where('id='.$id)->find());
			$this->assign('nodelist',M('Workflownode')->where('workflowid='.$id)->select());
            $this->display('checkDiagram');
		}
    }
    public function workflowlist(){
        $User = session('User');
	    //$Object->creater=$User[id];
        $object=M('Workflow');
		$list=$object->where("")
		->field('bs_Workflow.*')
		->join('INNER JOIN bs_workflownode b ON bs_Workflow.id = b.workflowid 
		    and b.type=0 
		    and bs_Workflow.status=1 
		    and (handlertype=2 and handlerobject1='.$User[id].' or handlertype=3 and handlerobject2='.$User[depid].' or handlertype=4)')
		->select();
        $this->assign('workflowlist',$list);
        $this->display();
    }
    public function editOperation(){
		$Object = M("Workflow");   
		$Object->create();
		$id = I('post.id');
		if($id){
			if($Object->save()){
				$this->success('修改成功',U('index'));
			}else{
				dump($Object->_sql());
				$this->error('修改失败');
			}
		}else{
			if($Object->add()){
				$this->success('添加成功',U('index'));
			}else{
				dump($Object->_sql());
				$this->error('添加失败');
			}
		}
    }
    public function removeOperation(){
		$Object = M("Workflow");
		if($Object->delete(I('get.id'))){
			$this->success('删除成功',U('index'));
		}else{
			dump($Object->_sql());
			$this->error('删除失败');
		}
    }	
    //商品编辑 by chenheyou
    public function open(){
		if(IS_POST){
			$id = I('post.id');
			$Object = M('Workflow')->where('id='.$id)->find();
			$ObjectSave = M("Workflow");   
			$ObjectSave->create($Object);
			$status = $Object[status];
			$json = array();
			if($status==1){
			    $ObjectSave->status=0;
				if($ObjectSave->save()){
					$json['state'] = 2;
				}
			}else{
			    $msg = '';
			    /*****************************************************************/
			    $count=M("Workflownode")->where(' type=0 and bs_Workflownode.workflowid='.$id)
        		->field(' count(*) as count')
        		->find();
			    if($count[count]!=1){
			         $msg = $msg.'创建节点有且仅有一个!';
			    }
			    /*****************************************************************/
			    $count = M("Workflownode")->where(' type=2 and bs_Workflownode.workflowid='.$id)
        		->field(' count(*) as count')
        		->find();
			    if($count[count]!=1){
			         $msg = $msg.'归档节点有且仅有一个!';
			    }
			    /*****************************************************************/
			    $count = M("Workflownode")->where(' type<>0 and handlertype in (3,4) and bs_Workflownode.workflowid='.$id)
        		->field(' count(*) as count')
        		->find();
			    if($count[count]>0){
			         $msg = $msg.'非创建节点操作者不可以是部门或所有人!';
			    }
			    /*****************************************************************/
			    $count = M("Workflownode")->where(' type=0 and handlertype in (0,1) and bs_Workflownode.workflowid='.$id)
        		->field(' count(*) as count')
        		->find();
			    if($count[count]>0){
			         $msg = $msg.'创建节点操作者设置错误!';
			    }
			    /*****************************************************************/
			    
			    

			    if($msg==''){
			        $count = M("Workflownode")->where('bs_Workflownode.workflowid='.$id)
			        ->field(' count(*) as count')
			        ->find();
			        //创建节点信息
			        $node = M("Workflownode")->where('type=0 and bs_Workflownode.workflowid='.$id)
			        ->field('*')
			        ->find();
			        //归档节点信息
			        $node2 = M("Workflownode")->where('type=2 and bs_Workflownode.workflowid='.$id)
			        ->field('*')
			        ->find();
			        $msg = $msg."无法到达归档节点";
			        for($i=1;$i<$count[count];$i=$i+1){
			            //echo  $nodeId[nextnodeid]."=====".$nodeId2.'_';
			            if($node[nextnodeid]==$node2[id]){
			                $msg = '';
			                break;
			            }
    			        $node = M("Workflownode")->where('id='.$node[nextnodeid].' and bs_Workflownode.workflowid='.$id)
    			        ->field('*')
    			        ->find();
			        }
			    }
			    
				if($msg==''){
    			    $ObjectSave->status=1;
    				if($ObjectSave->save()){
					   $json['state'] = 1;
    				}
				}else{
					$json['state'] = $msg;
				}
			}
			echo json_encode($json);exit;
		} else {
			$this->display();
		}
    }
}